package butines.core.game.lqdb;

import java.util.List;

import butines.core.game.GameEntity;

public class LocalityQueryFacility implements ProximityDatabase {

	private class LQTokenProximity implements ProximityToken {

		private SpatialDatabase.BinNode bin;
		
		private LQTokenProximity(Object object) {
			this.bin = database.createBin(object);
		}
		
		@SuppressWarnings("unchecked")
		public List<GameEntity> findEntities(float centerx, float centery, float radius) {
			return (List<GameEntity>)database.findObjects(centerx, centery, radius);
		}

		public void notify(float newx, float newy) {
			database.update(bin, newx, newy);
		}

		public void release() {
			database.remove(bin);
		}
		
	}
	
	private SpatialDatabase database;
	
	public LocalityQueryFacility(float originx, float originy, float sizex, float sizey, int divx, int divy) {
		database = new SpatialDatabase(originx, originy, sizex, sizey, divx, divy);
	}

	public ProximityToken createTokenProximity(GameEntity gameEntity) {
		return new LQTokenProximity(gameEntity);
	}
	
	@SuppressWarnings("unchecked")
	public List<GameEntity> findEntities(float centerx, float centery, float radius) {
		return (List<GameEntity>) database.findObjects(centerx, centery, radius);
	}
	
	@SuppressWarnings("unchecked")
	public List<GameEntity> findAllEntities() {
		return (List<GameEntity>) database.findAllObjects();
	}
	
//	public void annotation(G2D g2d) {
//		database.annotation(g2d);
//	}
	
}
